package us.nchu.tomtiger.service;

import org.springframework.stereotype.Service;
import us.nchu.tomtiger.dao.UserDao;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.model.UserPending;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public void createUser(User user) {
        userDao.insert(user);
    }

    public User getUserByPhoneNumber(String phoneNumber) {
        return userDao.query(phoneNumber);
    }

    public void updateUserInfo(User user) {
        userDao.update(user);
    }

    public void updateUserInfoForAdmin(User user) {
        userDao.updateAdmin(user);
    }

    public List<User> queryAllUser() {
        return userDao.queryAll();
    }

    public List<User> queryListByUserPendingList(List<UserPending> userPendingList) {
        List<User> userList = new ArrayList<>();
        if (userPendingList.size() == 0) {
            return userList;
        }
        else {
            for (int i = 0; i < userPendingList.size(); i++) {
                userList.add(getUserByPhoneNumber(userPendingList.get(i).getPhoneNumber()));
            }
        }
        return userList;
    }

    public void deleteUserByPhoneNumber(String phoneNumber) {
        userDao.delete(phoneNumber);
    }
}
